﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;

namespace ProtocolFactory
{
    public class ProtocolFactory
    {
        /// <summary>
        ///     创建协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类别
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="string">
        ///     <para>
        ///         命令类型
        ///     </para>
        /// </param>
        /// <param name="PROTOCOL" type="string">
        ///     <para>
        ///         命令协议簇
        ///     </para>
        /// </param>
        /// <param name="PackageBody" type="string">
        ///     <para>
        ///         主体数据
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回馈标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
       /// <param name="DIC_USERINFO">
        ///     <para>
        ///         自身基础会话码
        ///     </para> 
       /// </param>
        /// <returns>
        ///     返回完整协议
        /// </returns>
        public static byte[] GetProtocol(String TYPE, int COMMAND, int PROTOCOL, byte[] PackageBody, ACK ack, ENCRYPT encrypt, String SessionCode)
        {
            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(PROTOCOL);
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(PackageBody.Length);
            byte[] f6 = PackageBody;
            byte[] f7 = new byte[2];
            byte[] f8 = SessionCode == "" ? BitConverter.GetBytes(16) : BitConverter.GetBytes(SessionCode.Length);
            byte[] f9 = SessionCode == "" ? Encoding.Default.GetBytes("0000000000000000") : Encoding.Default.GetBytes(SessionCode);
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }

        /// <summary>
        ///     创建协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类别
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="string">
        ///     <para>
        ///         命令类型
        ///     </para>
        /// </param>
        /// <param name="PROTOCOL" type="string">
        ///     <para>
        ///         命令协议簇
        ///     </para>
        /// </param>
        /// <param name="PackageBody" type="string">
        ///     <para>
        ///         主体数据
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回馈标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回完整协议
        /// </returns>
        public static byte[] GetProtocol(String TYPE, int COMMAND, String PROTOCOL, String PackageBody, ACK ack, ENCRYPT encrypt)
        {
            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(int.Parse(PROTOCOL));
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(Encoding.Default.GetBytes(PackageBody).Length);
            byte[] f6 = Encoding.Default.GetBytes(PackageBody);
            byte[] f7 = new byte[2];
            byte[] f8 = BitConverter.GetBytes(16);
            byte[] f9 = Encoding.Default.GetBytes("0000000000000000");
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }

        public static String GetPictireProtocol(Image img, int PID, String Username)
        {
            String PicProtocol = "<PIC|";
            PicProtocol += "SOURCE(*Equ*)\'" + img.Tag.ToString() + "\"|";
            PicProtocol += "HEIGHT(*Equ*)\"" + img.Height + "\"|";
            PicProtocol += "WIDTH(*Equ*)\"" + img.Width + "\"|";
            PicProtocol += "PID(*Equ*)\"" + PID + "\"|";
            PicProtocol += "USERNAME(*Equ*)\"" + Username + "\"|";
            String[] ImgTag = img.ToolTip.ToString().Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            if (ImgTag.Length > 1)
            {
                if (ImgTag[1] == "sysface")
                {
                    PicProtocol += "PICTYPE(*Equ*)\"" + (int)PictrueType.Define + "\"|";
                }
                else
                {
                    PicProtocol += "PICTYPE(*Equ*)\"" + (int)PictrueType.User + "\"|";
                }
            }
            else
            {
                PicProtocol += "PICTYPE(*Equ*)\"" + (int)PictrueType.User + "\"|";
            }
            PicProtocol += "/>";
            return PicProtocol.Replace(" ", "(*Space*)");
        }

        public static String GetTextProtocol(Run inline)
        {
            String TextProtocol = "<FONT|";
            TextProtocol += "COLOR(*Equ*)\'" + inline.Foreground.ToString() + "\"|";
            TextProtocol += "FAMILY(*Equ*)\"" + inline.FontFamily.ToString() + "\"|";
            TextProtocol += "SIZE(*Equ*)\"" + inline.FontSize.ToString() + "\"|";
            TextProtocol += "VALUE(*Equ*)\"" + inline.Text.Replace("|", "(*Split*)").Replace("=","(*CEqu*)") + "\"|";
            TextProtocol += "/>";
            return TextProtocol.Replace(" ", "(*Space*)");
        }

        /// <summary>
        ///         产生聊天协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类别
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="int">
        ///     <para>
        ///         命令类型
        ///     </para>
        /// </param>
        /// <param name="PROTOCOL" type="string">
        ///     <para>
        ///         命令协议簇
        ///     </para>
        /// </param>
        /// <param name="PackageBody" type="string">
        ///     <para>
        ///         主体数据
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回馈标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="SessionCode" type="string">
        ///     <para>
        ///         会话标示
        ///     </para>
        /// </param>
        /// <returns>
        ///    返回完整聊天协议
        /// </returns>
        public static byte[] ReGetProtocolToTalkMessage(String TYPE, int COMMAND, int PROTOCOL, byte[] PackageBody, ACK ack, ENCRYPT encrypt, String SessionCode)
        {
            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(PROTOCOL);
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(PackageBody.Length);
            byte[] f6 = PackageBody;
            byte[] f7 = new byte[2];
            byte[] f8 = SessionCode == "" ? BitConverter.GetBytes(16) : BitConverter.GetBytes(SessionCode.Length);
            byte[] f9 = SessionCode == "" ? Encoding.Default.GetBytes("0000000000000000") : Encoding.Default.GetBytes(SessionCode);
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }

        /// <summary>
        ///     产生分布发送的聊天协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类型
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="int">
        ///     <para>
        ///         命令
        ///     </para>
        /// </param>
        /// <param name="PROTOCO" type="string">
        ///     <para>
        ///         协议
        ///     </para>
        /// </param>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="FriendUsername" type="string">
        ///     <para>
        ///         好友名
        ///     </para>
        /// </param>
        /// <param name="Session" type="string">
        ///     <para>
        ///         当前分包会话
        ///     </para>
        /// </param>
        /// <param name="DetachMessage" type="string">
        ///     <para>
        ///         分包信息
        ///     </para>
        /// </param>
        /// <param name="PackageId" type="int">
        ///     <para>
        ///         分包ID
        ///     </para>
        /// </param>
        /// <param name="TotalPackageCount" type="int">
        ///     <para>
        ///         包总数
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回复标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回分包传送的聊天协议
        /// </returns>
        public static byte[] DetachTalkMessageProtocol(String TYPE, int COMMAND, int PROTOCOL, byte[] username, byte[] FriendUsername, byte[] Session, byte[] DetachMessage, byte[] PackageId, byte[] TotalPackageCount, ACK ack, ENCRYPT encrypt)
        {
            #region 分布聊天数据组合头部

            byte[] UsernameLength = BitConverter.GetBytes(username.Length);
            byte[] FriendNameLength = BitConverter.GetBytes(FriendUsername.Length);
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] DetachMessageLength = BitConverter.GetBytes(DetachMessage.Length);
            byte[] packagebody = new byte[username.Length + FriendUsername.Length + Session.Length + DetachMessage.Length + 24];
            int Offset = 0;
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(username, 0, packagebody, Offset, username.Length);
            Offset += username.Length;
            Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(FriendUsername, 0, packagebody, Offset, FriendUsername.Length);
            Offset += FriendUsername.Length;
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            Array.ConstrainedCopy(DetachMessageLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(DetachMessage, 0, packagebody, Offset, DetachMessage.Length);
            Offset += DetachMessage.Length;
            Array.ConstrainedCopy(PackageId, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(TotalPackageCount, 0, packagebody, Offset, 4);
            Offset += 4;

            #endregion

            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(PROTOCOL);
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(packagebody.Length);
            byte[] f6 = packagebody;
            byte[] f7 = new byte[2];
            byte[] f8 = Session.Length <= 0 ? BitConverter.GetBytes(16) : BitConverter.GetBytes(Session.Length);
            byte[] f9 = Session.Length <= 0 ? Encoding.Default.GetBytes("0000000000000000") : Session;
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }

        /// <summary>
        ///     得到分布传输图片协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类型
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="int">
        ///     <para>
        ///         命令
        ///     </para>
        /// </param>
        /// <param name="PROTOCOL" type="string">
        ///     <para>
        ///         协议簇
        ///     </para>
        /// </param>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="FriendUsername" type="string">
        ///     <para>
        ///         好友用户名
        ///     </para>
        /// </param>
        /// <param name="TalkSession" type="string">
        ///     <para>
        ///         本次聊天会话ID
        ///     </para>
        /// </param>
        /// <param name="DetachImageData" type="byte[]">
        ///     <para>
        ///         图片分包数据
        ///     </para>
        /// </param>
        /// <param name="ImageDataCount" type="int">
        ///     <para>
        ///         本次图片分包大小
        ///     </para>
        /// </param>
        /// <param name="PID" type="string">
        ///     <para>
        ///         本次图片ID
        ///     </para>
        /// </param>
        /// <param name="PackageId" type="int">
        ///     <para>
        ///         当前封包片ID
        ///     </para>
        /// </param>
        /// <param name="TotalPackageCount" type="int">
        ///     <para>
        ///         封包总共封包片数目
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回馈标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回本次分包传输图片协议
        /// </returns>
        public static byte[] DetachImageProtocol(String TYPE, int COMMAND, int PROTOCOL, byte[] username, byte[] FriendUsername, byte[] TalkSession, byte[] DetachImageData, byte[] PID, byte[] TotalLength, byte[] PackageId, byte[] TotalPackageCount, ACK ack, ENCRYPT encrypt)
        {
            #region 分布图片数据组合头部

            byte[] UsernameLength = BitConverter.GetBytes(username.Length);
            byte[] FriendNameLength = BitConverter.GetBytes(FriendUsername.Length);
            byte[] SessionLength = BitConverter.GetBytes(TalkSession.Length);
            byte[] DetachImageLength = BitConverter.GetBytes(DetachImageData.Length);
            byte[] packagebody = new byte[username.Length + FriendUsername.Length + TalkSession.Length + DetachImageData.Length + 32];
            int Offset = 0;
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(username, 0, packagebody, Offset, username.Length);
            Offset += username.Length;
            Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(FriendUsername, 0, packagebody, Offset, FriendUsername.Length);
            Offset += FriendUsername.Length;
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(TalkSession, 0, packagebody, Offset, TalkSession.Length);
            Offset += TalkSession.Length;
            Array.ConstrainedCopy(DetachImageLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(DetachImageData, 0, packagebody, Offset, DetachImageData.Length);
            Offset += DetachImageData.Length;
            Array.ConstrainedCopy(PID, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(TotalLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(PackageId, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(TotalPackageCount, 0, packagebody, Offset, 4);
            Offset += 4;

            #endregion

            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(PROTOCOL);
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(packagebody.Length);
            byte[] f6 = packagebody;
            byte[] f7 = new byte[2];
            byte[] f8 = TalkSession.Length <= 0 ? BitConverter.GetBytes(16) : BitConverter.GetBytes(TalkSession.Length);
            byte[] f9 = TalkSession.Length <= 0 ? Encoding.Default.GetBytes("0000000000000000") : TalkSession;
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }

        /// <summary>
        ///         产生请求发送文件协议
        /// </summary>
        /// <param name="TYPE" type="string">
        ///     <para>
        ///         类别
        ///     </para>
        /// </param>
        /// <param name="COMMAND" type="int">
        ///     <para>
        ///         命令类型
        ///     </para>
        /// </param>
        /// <param name="PROTOCOL" type="string">
        ///     <para>
        ///         命令协议簇
        ///     </para>
        /// </param>
        /// <param name="PackageBody" type="string">
        ///     <para>
        ///         主体数据
        ///     </para>
        /// </param>
        /// <param name="ack" type="ProtocolFactory.ProtocolFactory.ACK">
        ///     <para>
        ///         回馈标示
        ///     </para>
        /// </param>
        /// <param name="encrypt" type="ProtocolFactory.ProtocolFactory.ENCRYPT">
        ///     <para>
        ///         加密标示
        ///     </para>
        /// </param>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="SessionCode" type="string">
        ///     <para>
        ///         会话标示
        ///     </para>
        /// </param>
        /// <returns>
        ///    返回完整聊天协议
        /// </returns>
        public static byte[] GetProtocolFromSendFile(String TYPE, int COMMAND, int PROTOCOL, byte[] PackageBody, ACK ack, ENCRYPT encrypt, String SessionCode)
        {
            byte[] f1 = Encoding.Default.GetBytes("BMSG");
            byte[] f2 = BitConverter.GetBytes(PROTOCOL);
            byte[] f3 = BitConverter.GetBytes(COMMAND);
            byte[] f4 = new byte[2];
            byte[] f5 = BitConverter.GetBytes(PackageBody.Length);
            byte[] f6 = PackageBody;
            byte[] f7 = new byte[2];
            byte[] f8 = SessionCode == "" ? BitConverter.GetBytes(16) : BitConverter.GetBytes(SessionCode.Length);
            byte[] f9 = SessionCode == "" ? Encoding.Default.GetBytes("0000000000000000") : Encoding.Default.GetBytes(SessionCode);
            byte[] f10 = new byte[2];
            byte[] f11 = BitConverter.GetBytes(0);
            byte[] f12 = BitConverter.GetBytes(Encoding.Default.GetBytes(TYPE).Length);
            byte[] f13 = Encoding.Default.GetBytes(TYPE);
            byte[] f14 = new byte[2];
            byte[] f15 = BitConverter.GetBytes((int)encrypt);
            byte[] f16 = BitConverter.GetBytes((int)ack);
            byte[] f17 = Encoding.Default.GetBytes("€");

            byte[] Total = new byte[f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length + f17.Length];
            Array.Copy(f1, 0, Total, 0, f1.Length);
            Array.Copy(f2, 0, Total, f1.Length, f2.Length);
            Array.Copy(f3, 0, Total, f1.Length + f2.Length, f3.Length);
            Array.Copy(f4, 0, Total, f1.Length + f2.Length + f3.Length, f4.Length);
            Array.Copy(f5, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length, f5.Length);
            Array.Copy(f6, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length, f6.Length);
            Array.Copy(f7, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length, f7.Length);
            Array.Copy(f8, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length, f8.Length);
            Array.Copy(f9, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length, f9.Length);
            Array.Copy(f10, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length, f10.Length);
            Array.Copy(f11, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length, f11.Length);
            Array.Copy(f12, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length, f12.Length);
            Array.Copy(f13, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length, f13.Length);
            Array.Copy(f14, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length, f14.Length);
            Array.Copy(f15, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length, f15.Length);
            Array.Copy(f16, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length, f16.Length);
            Array.Copy(f17, 0, Total, f1.Length + f2.Length + f3.Length + f4.Length + f5.Length + f6.Length + f7.Length + f8.Length + f9.Length
             + f10.Length + f11.Length + f12.Length + f13.Length + f14.Length + f15.Length + f16.Length, f17.Length);

            return Total;
        }


        /// <summary>
        ///     回馈标示
        /// </summary>
        public enum ACK
        {
            /// <summary>
            ///     需要回馈
            /// </summary>
            Yes,
            /// <summary>
            ///     不需要回馈
            /// </summary>
            No
        }

        /// <summary>
        ///     加密标示
        /// </summary>
        /// <remarks>
        ///     
        /// </remarks>
        public enum ENCRYPT
        {
            /// <summary>
            ///     需要加密
            /// </summary>
            Yes,
            /// <summary>
            ///     不需要加密
            /// </summary>
            No
        }

        /// <summary>
        ///         图片种类枚举
        /// </summary>
        public enum PictrueType
        {
            /// <summary>
            ///         系统默认图片 -  默认表情
            /// </summary>
            Define,
            /// <summary>
            ///         用户自定义图片
            /// </summary>
            User
        }
    }
}
